package com.taxx.controller;

import com.taxx.common.Result;
import com.taxx.entity.SysPermission;
import com.taxx.service.SysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统权限管理控制器
 */
@RestController
@RequestMapping("/permissions")
@CrossOrigin
public class SysPermissionController {
    
    @Autowired
    private SysPermissionService permissionService;
    
    /**
     * 获取权限列表
     */
    @GetMapping("/list")
    public Result<List<SysPermission>> getPermissionList() {
        try {
            List<SysPermission> permissions = permissionService.getPermissionList();
            return Result.success("获取权限列表成功", permissions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据ID获取权限信息
     */
    @GetMapping("/{id}")
    public Result<SysPermission> getPermissionById(@PathVariable Long id) {
        try {
            SysPermission permission = permissionService.getPermissionById(id);
            if (permission == null) {
                return Result.notFound("权限不存在");
            }
            return Result.success("获取权限信息成功", permission);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 创建权限
     */
    @PostMapping
    public Result<Void> createPermission(@Validated @RequestBody SysPermission permission) {
        try {
            boolean success = permissionService.createPermission(permission);
            if (success) {
                return Result.success();
            } else {
                return Result.error("权限创建失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新权限
     */
    @PutMapping("/{id}")
    public Result<Void> updatePermission(@PathVariable Long id, @Validated @RequestBody SysPermission permission) {
        try {
            permission.setId(id);
            boolean success = permissionService.updatePermission(permission);
            if (success) {
                return Result.success();
            } else {
                return Result.error("权限更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除权限
     */
    @DeleteMapping("/{id}")
    public Result<Void> deletePermission(@PathVariable Long id) {
        try {
            boolean success = permissionService.deletePermission(id);
            if (success) {
                return Result.success();
            } else {
                return Result.error("权限删除失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新权限状态
     */
    @PutMapping("/{id}/status")
    public Result<Void> updatePermissionStatus(@PathVariable Long id, @RequestParam Integer status) {
        try {
            boolean success = permissionService.updatePermissionStatus(id, status);
            if (success) {
                return Result.success();
            } else {
                return Result.error("权限状态更新失败");
            }
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据角色ID获取权限列表
     */
    @GetMapping("/role/{roleId}")
    public Result<List<SysPermission>> getPermissionsByRoleId(@PathVariable Long roleId) {
        try {
            List<SysPermission> permissions = permissionService.getPermissionsByRoleId(roleId);
            return Result.success("获取角色权限成功", permissions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取当前用户的权限列表
     */
    @GetMapping("/user")
    public Result<List<SysPermission>> getCurrentUserPermissions() {
        try {
            // 从SecurityContext获取当前用户信息
            String username = org.springframework.security.core.context.SecurityContextHolder
                    .getContext().getAuthentication().getName();
            
            if (username == null || "anonymousUser".equals(username)) {
                return Result.error("用户未登录");
            }
            
            List<SysPermission> permissions = permissionService.getPermissionsByUsername(username);
            return Result.success("获取用户权限成功", permissions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 根据用户ID获取权限列表
     */
    @GetMapping("/user/{userId}")
    public Result<List<SysPermission>> getPermissionsByUserId(@PathVariable Long userId) {
        try {
            List<SysPermission> permissions = permissionService.getPermissionsByUserId(userId);
            return Result.success("获取用户权限成功", permissions);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
